package vow.gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;

import agents.TheWatcher;

import ontology.models.contract.FulFill;
import ontology.models.contract.Obligation;
import ontology.models.contract.Violation;
import ruleengine.JasperUtil;

public class ObligationOptions extends JPopupMenu{

	private Obligation selected;
	private TheWatcher agent;
	
	public ObligationOptions(Obligation o, TheWatcher agent){
		selected = o;
		this.agent = agent;
		initComponents();
	}
	
	private void initComponents(){
		JMenuItem fulfill = new JMenuItem("realizada");
		fulfill.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				fulfillObligation();
			}
		});
		
		JMenuItem violated = new JMenuItem("violada");
		violated.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				violateObligation();
			}
		});
		
		add(fulfill);
		add(violated);
	}
	
	private void fulfillObligation(){
		long now = System.currentTimeMillis();
		FulFill f = new FulFill();
		f.setAction(selected.getAction());
		f.setWhen(now+"");
		f.setContractId(selected.getContractId());
		agent.performAction(f);
	}
	
	private void violateObligation(){
		long now = System.currentTimeMillis();
		Violation violation = new Violation();
		violation.setAction(selected.getAction());
		violation.setWhen(selected.getWhen());
		violation.setContractId(selected.getContractId());
		agent.performAction(violation);
	}
}
